package com.em.controller;

import com.em.entity.DeviceInspection;
import com.em.entity.Fee;
import com.em.service.DeviceInspectionService;
import com.em.service.FeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.math.BigDecimal;
import java.util.Collections;
import java.util.List;

@Controller
@RequestMapping("/fee")
public class FeeController {
    private static String PAGE = "/fee";
    private static String REDIRECT_PAGE = "redirect:" + PAGE;

    @Autowired
    private FeeService service;

    @GetMapping("/list")
    public String list(@RequestParam(value = "name", defaultValue = "") String name,
                       @RequestParam(value = "page", defaultValue = "1") int pageNumber,
                       Model model) {
        int pageSize = 10;
        List<Fee> list = service.select(name);
        int totalSize = list.size();
        int start = (pageNumber - 1) * pageSize;
        if (start > totalSize) {
            list = Collections.emptyList();
        } else {
            // 获取分页数据
            list = list.subList(start, Math.min(start + pageSize, totalSize));
        }
        model.addAttribute("list", list);
        model.addAttribute("pageNumber", pageNumber);
        model.addAttribute("totalPages", list.size());
        return PAGE + "/list";
    }

    @PostMapping("/add")
    public String add(@RequestParam Integer residentId,
                      @RequestParam String feeType,
                      @RequestParam BigDecimal amount,
                      @RequestParam String dueDate,
                      @RequestParam(required = false) String paidDate,
                      @RequestParam String status) {
        Fee fee = new Fee();
        fee.setResidentId(residentId);
        fee.setFeeType(feeType);
        fee.setAmount(amount);
        fee.setDueDate(dueDate);
        fee.setPaidDate(paidDate);
        fee.setStatus(status);
        service.create(fee);
        return REDIRECT_PAGE + "/list";
    }

    @GetMapping("/get/{id}")
    @ResponseBody
    public Fee getResident(@PathVariable Integer id) {
        return service.selectById(id);
    }

    @PostMapping("/update")
    public String update(@RequestParam Integer id,
                         @RequestParam Integer residentId,
                         @RequestParam String feeType,
                         @RequestParam BigDecimal amount,
                         @RequestParam String dueDate,
                         @RequestParam(required = false) String paidDate,
                         @RequestParam String status) {
        Fee fee = new Fee();
        fee.setId(id);
        fee.setResidentId(residentId);
        fee.setFeeType(feeType);
        fee.setAmount(amount);
        fee.setDueDate(dueDate);
        fee.setPaidDate(paidDate);
        fee.setStatus(status);
        service.update(fee);
        return REDIRECT_PAGE + "/list";
    }


    @GetMapping("/delete/{id}")
    public String delete(@PathVariable("id") Integer id) {
        service.delete(id);
        return REDIRECT_PAGE + "/list";
    }
}
